package exercise;

import JZOffer.ListNode;

public class LC206 {
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }

        ListNode prev = head;
        ListNode cur = head.next;
        prev.next = null;

        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = prev;
            prev = cur;
            cur = curNext;
        }

        return prev;
    }

    public static void main(String[] args) {
        LC206 lc206 = new LC206();
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(4);

        ListNode node = lc206.reverseList(head);
        while (node != null) {
            System.out.println(node.val);
            node = node.next;
        }
    }
}
